﻿using dotnet_graphql_demo.Model;
using Microsoft.EntityFrameworkCore;

namespace dotnet_graphql_demo
{
    public class AppDbContext:DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }

        public virtual DbSet<Owner> Owners { get; set; }
        public virtual DbSet<Account> Accounts { get; set; }


        /// <summary>
        /// 重写实体属性
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Account>(entity =>
            {
                entity.HasOne(d => d.Owner)
                    .WithMany(p => p.Accounts)
                    .HasForeignKey(d => d.OwnerId)
                    .OnDelete(DeleteBehavior.Restrict)
                    .HasConstraintName("FK_Owner_Account");
            });

            base.OnModelCreating(modelBuilder);
        }
    }
}
